#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : rewrite_worker.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/4/26 17:16
"""

import json
import traceback
from PyQt5.QtCore import QThread, pyqtSignal
from utils.tools import request
from config import Config


class RewriteWorkerQtThread(QThread):
    """重写文案"""
    result = pyqtSignal(str)
    failed = pyqtSignal(Exception)

    def __init__(self, content, headers, parent=None):
        super(RewriteWorkerQtThread, self).__init__()
        self.content = content
        self.headers = headers

    def run(self):
        try:
            data = {
                "topic": "",
                "t": "",
                "k": "",
                "other": self.content + "根据这段文字重写文案要求要跟这段文字相关"
            }
            url = f"{Config.Host}/api/write/xhs/"
            self.headers.update({'Content-Type': 'application/json'})
            response = request(url, method='POST', headers=self.headers, data=json.dumps(data))
            response.raise_for_status()
            context = response.json()["data"]["content"]
            self.result.emit(context)
        except Exception as e:
            traceback.print_exc()
            self.failed.emit(e)
